package com.lagou.hdfs.mapreduce.sort;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class SortMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {

    //提升为全局变量，避免每次执行map方法都执行此操作
    final LongWritable word = new LongWritable();
    final LongWritable one = new LongWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //        1 接收到文本内容，转为String类型
        final String str = value.toString();
//        2 按照空格进行切分
        final String[] words = str.split(" ");
        for (String s : words) {
            try {
                Long num = Long.parseLong(s);
                word.set(num);
                context.write(one, word);
            } catch (NumberFormatException e) {
                System.out.println("numberFormatException");
            }
        }
    }
}
